package cn.dengqiang.order.service.impl;

import cn.dengqiang.basic.service.impl.BaseServiceImpl;
import cn.dengqiang.basic.util.CodeGenerateUtils;
import cn.dengqiang.basic.util.PageList;
import cn.dengqiang.order.domain.AdoptOrder;
import cn.dengqiang.order.domain.OrderAddress;
import cn.dengqiang.order.mapper.AdoptOrderMapper;
import cn.dengqiang.order.mapper.OrderAddressMapper;
import cn.dengqiang.order.query.AdoptOrderQuery;
import cn.dengqiang.order.service.IAdoptOrderService;
import cn.dengqiang.org.domain.Employee;
import cn.dengqiang.org.mapper.EmployeeMapper;
import cn.dengqiang.pay.constants.PayConstants;
import cn.dengqiang.pet.domain.Pet;
import cn.dengqiang.pet.mapper.PetMapper;
import cn.dengqiang.user.domain.LoginInfo;
import cn.dengqiang.user.domain.User;
import cn.dengqiang.user.domain.UserAddress;
import cn.dengqiang.user.mapper.UserAddressMapper;
import cn.dengqiang.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
public class AdoptOrderServiceImpl extends BaseServiceImpl<AdoptOrder> implements IAdoptOrderService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AdoptOrderMapper adoptOrderMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IPayBillService payBillService;
    @Override
    public String submit(Map<String, Object> params, LoginInfo loginInfo) {
        Long petId = Long.valueOf(params.get("pet_id").toString());//宠物id
        Long addressId = Long.valueOf(params.get("address_id").toString());//地址id
        Integer serviceMethod = Integer.valueOf(params.get("service_method").toString());//配送方式
        Long payMethod = Long.valueOf(params.get("pay_method").toString());//支付方式
        Pet pet = petMapper.loadById(petId);//拿到当前宠物的信息

        //修改宠物状态
        pet.setState(0);
        pet.setOffsaletime(new Date());
        pet.setShop_id(pet.getShop().getId());

        //绑定用户
        User user = userMapper.loadByLoginfoId(loginInfo.getId());
        pet.setUser(user);
        pet.setUser_id(user.getId());
        petMapper.update(pet);

        //创建领养订单
        //创建订单本身
        String unionPaySn = CodeGenerateUtils.generateUnionPaySn();
        AdoptOrder adoptOrder = initAdoptOrder(user, pet);
        adoptOrder.setPaySn(unionPaySn);
        adoptOrderMapper.save(adoptOrder);

        //创建订单地址
        UserAddress userAddress = userAddressMapper.loadById(addressId);
        OrderAddress orderAddress = userAddress2OrderAddress(userAddress, adoptOrder);
        orderAddressMapper.save(orderAddress);

        //创建支付单
        PayBill payBill = new PayBill();
        payBill.setDigest(adoptOrder.getDigest());
        payBill.setMoney(adoptOrder.getPrice());
        payBill.setUnionPaySn(adoptOrder.getPaySn());
        payBill.setState(0);
        payBill.setLastPayTime(new Date(System.currentTimeMillis()+ PayConstants.ORDER_TIMEOUT));

        payBill.setPayChannel(payMethod);//0 余额 1 支付宝 2 微信 3 银联
        payBill.setBusinessType(PayConstants.BUSINESS_TYPE_ADOPT);

        payBill.setBusinessKey(adoptOrder.getId());

        payBill.setUser_id(user.getId());
        payBill.setShop_id(pet.getShop().getId());
        payBill.setNickName(user.getUsername());
        payBillMapper.save(payBill);
        String payData = payBillService.payRequest(payBill);
        return payData;
    }

    @Override
    public PageList<AdoptOrder> queryUserOrder(AdoptOrderQuery query, LoginInfo loginInfo) {
        User user = userMapper.loadByLoginfoId(loginInfo.getId());
        query.setUserId(user.getId());
        return super.queryPage(query);
    }


    //如果我们不传入shopId就是平台管理员，否则就是店铺管理员或员工
    @Override
    public PageList<AdoptOrder> queryAdminOrder(AdoptOrderQuery query, LoginInfo loginInfo) {
        Employee employee = employeeMapper.loadByLoginInfoId(loginInfo.getId());
        query.setShopId(employee.getShop_id());
        return super.queryPage(query);
    }

    public OrderAddress userAddress2OrderAddress(UserAddress userAddress, AdoptOrder adoptOrder){
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress,orderAddress);
        orderAddress.setId(null);
        orderAddress.setOrder_id(adoptOrder.getId());
        orderAddress.setOrderSn(adoptOrder.getOrderSn());
        return orderAddress;
    }

    public AdoptOrder initAdoptOrder(User user,Pet pet){
        AdoptOrder adoptOrder = new AdoptOrder();
        adoptOrder.setDigest("[摘要]领养"+pet.getName()+"的订单！");
        adoptOrder.setState(0); //待支付
        adoptOrder.setPrice(pet.getSaleprice()); //待支付
        adoptOrder.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        //lastPayTime最后支付时间  当前时间+15分钟
        Date lastPayTime = new Date(System.currentTimeMillis()+15*60*1000);
        adoptOrder.setLastPayTime(lastPayTime);
        adoptOrder.setPet_id(pet.getId());
        adoptOrder.setUser_id(user.getId());
        adoptOrder.setShop_id(pet.getShop_id());
        return adoptOrder;
    }
}
